# Replication file for the supplementary appendix to
# ``Ideology and Specific Support for the Supreme Court''
# Haglin, Jordan, Merrill, and Ura

# CCESes.dta is just a merged file of each of the individual CCES years. 
# They were retrieved by the authors May 2020 from https://cces.gov.harvard.edu/

# July 21, 2020

library(readstata13)
library(ggplot2)
library(reshape)
library(gridExtra)
library(ggpubr)


rm(list = ls())

data <- read.dta13("/Users/scj0014/Myfiles/Research/SCOTUS Approval/CCESes.dta")

scotus.2018.dist2 <- lm(supapprove_NA ~ congapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	scotus.toocons + scotus.toolib + 
	scotus.dist2 + scotus.toocons:scotus.dist2,
	data = data, subset = (theyear == 2018))
summary(scotus.2018.dist2)
# Dist2: effect of dist 2 for lib

scotus.2017.dist2 <- lm(supapprove_NA ~ congapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	scotus.toocons + scotus.toolib + 
	scotus.dist2 + scotus.toocons:scotus.dist2,
	data = data, subset = (theyear == 2017))
summary(scotus.2017.dist2)
# Dist2: effect of dist 2 for lib

scotus.2016.dist2 <- lm(supapprove_NA ~ congapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	scotus.toocons + scotus.toolib + 
	scotus.dist2 + scotus.toocons:scotus.dist2,
	data = data, subset = (theyear == 2016))
summary(scotus.2016.dist2)
# Dist2: effect of dist 2 for lib

scotus.2015.dist2 <- lm(supapprove_NA ~ congapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	scotus.toocons + scotus.toolib + 
	scotus.dist2 + scotus.toocons:scotus.dist2,
	data = data, subset = (theyear == 2015))
summary(scotus.2015.dist2)
# Dist2: effect of dist 2 for lib

scotus.2014.dist2 <- lm(supapprove_NA ~ congapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	scotus.toocons + scotus.toolib + 
	scotus.dist2 + scotus.toocons:scotus.dist2,
	data = data, subset = (theyear == 2014))
summary(scotus.2014.dist2)
# Dist2: effect of dist 2 for lib

scotus.2013.dist2 <- lm(supapprove_NA ~ congapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	scotus.toocons + scotus.toolib + 
	scotus.dist2 + scotus.toocons:scotus.dist2,
	data = data, subset = (theyear == 2013))
summary(scotus.2013.dist2)
# Dist2: effect of dist 2 for lib

scotus.2012.dist2 <- lm(supapprove_NA ~ congapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	scotus.toocons + scotus.toolib + 
	scotus.dist2 + scotus.toocons:scotus.dist2,
	data = data, subset = (theyear == 2012))
summary(scotus.2012.dist2)
# Dist2: effect of dist 2 for lib

						
pred.data <- data.frame(congapprove_NA = mean(data$congapprove_NA, na.rm = T), presapprove_NA = mean(data$presapprove_NA, na.rm = T),
						bornagain = 0, female = 1, age = mean(data$age, na.rm = T), 
						income = mean(data$income, na.rm = T), educ = mean(data$educ, na.rm = T), 
						# Dems, Indps, # Reps, 49 comb each one
						pid = c(rep(1, 49), rep(4, 49), rep(7, 49)),
						selfideo_NA = c(rep(1, 7), rep(2, 7), rep(3, 7), rep(4, 7), rep(5, 7), rep(6, 7), rep(7, 7)),
						scotusideo = seq(1, 7, 1))


pred.data$scotus.toocons <- ifelse(pred.data$selfideo_NA < pred.data$scotusideo, 1, 0)
pred.data$scotus.toolib <- ifelse(pred.data$selfideo_NA > pred.data$scotusideo, 1, 0)						
pred.data$scotus.dist <- pred.data$selfideo_NA - pred.data$scotusideo
pred.data$scotus.dist2 <- pred.data$scotus.dist*pred.data$scotus.dist


preds.2018 <- data.frame(pred.data, predict(scotus.2018.dist2, pred.data, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2018)

preds.2017 <- data.frame(pred.data, predict(scotus.2017.dist2, pred.data, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2017)

preds.2016 <- data.frame(pred.data, predict(scotus.2016.dist2, pred.data, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2016)

preds.2015 <- data.frame(pred.data, predict(scotus.2015.dist2, pred.data, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2015)

preds.2014 <- data.frame(pred.data, predict(scotus.2014.dist2, pred.data, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2014)

preds.2013 <- data.frame(pred.data, predict(scotus.2013.dist2, pred.data, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2013)

preds.2012 <- data.frame(pred.data, predict(scotus.2012.dist2, pred.data, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2012)

all.preds <- rbind(preds.2018, preds.2017, preds.2016, preds.2015, preds.2014, preds.2013, preds.2012)


all.preds.small <- all.preds[all.preds$selfideo_NA %in% c(1, 4, 7), ]
all.preds.small$scotusdistcopy <- all.preds.small$scotus.dist

all.preds.small$Ideology <- all.preds.small$selfideo_NA
all.preds.small$Ideology[all.preds.small$Ideology == 1] <- "Very Liberal"
all.preds.small$Ideology[all.preds.small$Ideology == 4] <- "Moderate"
all.preds.small$Ideology[all.preds.small$Ideology == 7] <- "Very Conservative"

all.preds.smaller <- all.preds.small[all.preds.small$pid %in% c(1, 4, 7), ]
all.preds.smaller$PID <- all.preds.smaller$pid
all.preds.smaller$PID[all.preds.smaller$PID == 1] <- "Strong Democrat"
all.preds.smaller$PID[all.preds.smaller$PID == 4] <- "Independent"
all.preds.smaller$PID[all.preds.smaller$PID == 7] <- "Strong Republican"

# Reorder for plotting levels in ggplot
all.preds.smaller$PID <- factor(all.preds.smaller$PID, levels = c("Strong Democrat", "Independent", "Strong Republican"))
all.preds.smaller$Ideology <- factor(all.preds.smaller$Ideology, levels = c("Very Liberal", "Moderate", "Very Conservative"))

all.preds.smallest <- all.preds.smaller[all.preds.smaller$scotus.dist %in% c(-6, -3, 0, 3, 6),]

## For plotting, we will organize by Ideology. Ideologyorg will be ``fake'' x values to make the plot. 
#   Ideology 1 will be -4 to set it far left. Moderate will be 0. Very conservative will be 4 to set it far right.
#  Then shift the SCOTUS dists up or down by one unit to create the trio.
all.preds.smallest$Ideologyorg <- all.preds.smallest$selfideo_NA
all.preds.smallest$Ideologyorg <- all.preds.smallest$Ideologyorg - 4

all.preds.smallest$Ideologyorg[all.preds.smallest$scotusdistcopy == -6 & all.preds.smallest$Ideologyorg == -3] <- -4
all.preds.smallest$Ideologyorg[all.preds.smallest$scotusdistcopy == 0 & all.preds.smallest$Ideologyorg == -3] <- -2

all.preds.smallest$Ideologyorg[all.preds.smallest$scotusdistcopy == -3 & all.preds.smallest$Ideologyorg == 0] <- -1
all.preds.smallest$Ideologyorg[all.preds.smallest$scotusdistcopy == 3 & all.preds.smallest$Ideologyorg == 0] <- 1

all.preds.smallest$Ideologyorg[all.preds.smallest$scotusdistcopy == 0 & all.preds.smallest$Ideologyorg == 3] <- 2
all.preds.smallest$Ideologyorg[all.preds.smallest$scotusdistcopy == 6 & all.preds.smallest$Ideologyorg == 3] <- 4

# Now shift the party ids to make a clump.
all.preds.smallest$Ideologyorg[all.preds.smallest$pid == 1] <- all.preds.smallest$Ideologyorg[all.preds.smallest$pid == 1] - 0.3
all.preds.smallest$Ideologyorg[all.preds.smallest$pid == 7] <- all.preds.smallest$Ideologyorg[all.preds.smallest$pid == 7] + 0.3

all.preds.smallest$SCOTUS <- all.preds.smallest$scotusdistcopy
all.preds.smallest$SCOTUS[all.preds.smallest$SCOTUS == -6] <- "Way Too Conservative"
all.preds.smallest$SCOTUS[all.preds.smallest$SCOTUS == -3] <- "Too Conservative"
all.preds.smallest$SCOTUS[all.preds.smallest$SCOTUS == 0] <- "Just Right"
all.preds.smallest$SCOTUS[all.preds.smallest$SCOTUS == 3] <- "Too Liberal"
all.preds.smallest$SCOTUS[all.preds.smallest$SCOTUS == 6] <- "Way Too Liberal"

all.preds.smallest$SCOTUS <- factor(all.preds.smallest$SCOTUS, levels = c("Way Too Conservative", "Too Conservative", "Just Right", "Too Liberal", "Way Too Liberal"))

all.preds.smallest$Ideologyorgmore <- all.preds.smallest$Ideologyorg
all.preds.smallest$Ideologyorgmore[all.preds.smallest$Ideologyorgmore < -1.5] <- all.preds.smallest$Ideologyorgmore[all.preds.smallest$Ideologyorgmore < -1.5] - 1
all.preds.smallest$Ideologyorgmore[all.preds.smallest$Ideologyorgmore > 1.5] <- all.preds.smallest$Ideologyorgmore[all.preds.smallest$Ideologyorgmore > 1.5] + 1

all.preds.smallest.2012 <- all.preds.smallest[all.preds.smallest$year == 2012,]
all.preds.smallest.2013 <- all.preds.smallest[all.preds.smallest$year == 2013,]
all.preds.smallest.2014 <- all.preds.smallest[all.preds.smallest$year == 2014,]
all.preds.smallest.2015 <- all.preds.smallest[all.preds.smallest$year == 2015,]
all.preds.smallest.2016 <- all.preds.smallest[all.preds.smallest$year == 2016,]
all.preds.smallest.2017 <- all.preds.smallest[all.preds.smallest$year == 2017,]
all.preds.smallest.2018 <- all.preds.smallest[all.preds.smallest$year == 2018,]

scotus.2012 <- ggplot(data = all.preds.smallest.2012, aes(Ideologyorgmore, fit.fit, shape = SCOTUS, color = PID)) + 
	ylim(0.85, 2.7) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "SCOTUS Distance", x = "Self Ideology", y = "Predicted SCOTUS Approval: 2012")

scotus.2013 <- ggplot(data = all.preds.smallest.2013, aes(Ideologyorgmore, fit.fit, shape = SCOTUS, color = PID)) + 
	ylim(0.85, 2.7) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "SCOTUS Distance", x = "Self Ideology", y = "Predicted SCOTUS Approval: 2013")

scotus.2014 <- ggplot(data = all.preds.smallest.2014, aes(Ideologyorgmore, fit.fit, shape = SCOTUS, color = PID)) + 
	ylim(0.85, 2.7) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "SCOTUS Distance", x = "Self Ideology", y = "Predicted SCOTUS Approval: 2014")

scotus.2015 <- ggplot(data = all.preds.smallest.2015, aes(Ideologyorgmore, fit.fit, shape = SCOTUS, color = PID)) + 
	ylim(1, 2.8) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "SCOTUS Distance", x = "Self Ideology", y = "Predicted SCOTUS Approval: 2015")

scotus.2016 <- ggplot(data = all.preds.smallest.2016, aes(Ideologyorgmore, fit.fit, shape = SCOTUS, color = PID)) + 
	ylim(1, 2.8) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "SCOTUS Distance", x = "Self Ideology", y = "Predicted SCOTUS Approval: 2016")

scotus.2017 <- ggplot(data = all.preds.smallest.2017, aes(Ideologyorgmore, fit.fit, shape = SCOTUS, color = PID)) + 
	ylim(1, 2.8) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "SCOTUS Distance", x = "Self Ideology", y = "Predicted SCOTUS Approval: 2017")

scotus.2018 <- ggplot(data = all.preds.smallest.2018, aes(Ideologyorgmore, fit.fit, shape = SCOTUS, color = PID)) + 
	ylim(1, 2.8) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 	
	#scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("Very Liberal", "Moderate", "Very Conservative")) + 
	labs(color = "Party ID", shape = "SCOTUS Distance", x = "Self Ideology", y = "Predicted SCOTUS Approval: 2018")


#pdf("/Users/scj0014/Myfiles/Research/SCOTUS Approval/scotusappsNA.pdf")
ggarrange(scotus.2015, scotus.2016, scotus.2017, scotus.2018,
	ncol = 2, nrow = 2, common.legend = TRUE, legend = "right")
dev.off()

library(stargazer)
stargazer(scotus.2015.dist2, scotus.2016.dist2, scotus.2017.dist2, scotus.2018.dist2, 
	covariate.labels = c("House Approval", "President Approval", 
		"Born Again", "Female", "Age", "Income", "Education", "Party ID",
		"Self Ideology (Liberal)", "SCOTUS Too Conservative (Dummy)", 
		"SCOTUS Too Liberal (Dummy)", "SCOTUS Distance (Squared)",
		"SCOTUS Too Conservative (Dummy): SCOTUS Distance (Squared)"))

########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################


rm(list = ls())

data <- read.dta13("/Users/scj0014/Myfiles/Research/SCOTUS Approval/CCESes.dta")


cong.2018.dist2 <- lm(congapprove_NA ~ supapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	house.toocons + house.toolib + 
	house.dist2 + house.toocons:house.dist2,
	data = data, subset = (theyear == 2018))
summary(cong.2018.dist2)
# Dist2: effect of dist 2 for lib

cong.2017.dist2 <- lm(congapprove_NA ~ supapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	house.toocons + house.toolib + 
	house.dist2 + house.toocons:house.dist2,
	data = data, subset = (theyear == 2017))
summary(cong.2017.dist2)
# Dist2: effect of dist 2 for lib

cong.2016.dist2 <- lm(congapprove_NA ~ supapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	house.toocons + house.toolib + 
	house.dist2 + house.toocons:house.dist2,
	data = data, subset = (theyear == 2016))
summary(cong.2016.dist2)
# Dist2: effect of dist 2 for lib

cong.2015.dist2 <- lm(congapprove_NA ~ supapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	house.toocons + house.toolib + 
	house.dist2 + house.toocons:house.dist2,
	data = data, subset = (theyear == 2015))
summary(cong.2015.dist2)
# Dist2: effect of dist 2 for lib

cong.2014.dist2 <- lm(congapprove_NA ~ supapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	house.toocons + house.toolib + 
	house.dist2 + house.toocons:house.dist2,
	data = data, subset = (theyear == 2014))
summary(cong.2014.dist2)
# Dist2: effect of dist 2 for lib

cong.2013.dist2 <- lm(congapprove_NA ~ supapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	house.toocons + house.toolib + 
	house.dist2 + house.toocons:house.dist2,
	data = data, subset = (theyear == 2013))
summary(cong.2013.dist2)
# Dist2: effect of dist 2 for lib

cong.2012.dist2 <- lm(congapprove_NA ~ supapprove_NA + presapprove_NA + 
	bornagain + female + age + income + educ + pid + selfideo_NA +
	house.toocons + house.toolib + 
	house.dist2 + house.toocons:house.dist2,
	data = data, subset = (theyear == 2012))
summary(cong.2012.dist2)
# Dist2: effect of dist 2 for lib





pred.data.cong <- data.frame(supapprove_NA = mean(data$supapprove_NA, na.rm = T), presapprove_NA = mean(data$presapprove_NA, na.rm = T),
						bornagain = 0, female = 1, age = mean(data$age, na.rm = T), 
						income = mean(data$income, na.rm = T), educ = mean(data$educ, na.rm = T), 
						# Dems, Indps, # Reps, 49 comb each one
						pid = c(rep(1, 49), rep(4, 49), rep(7, 49)),
						selfideo_NA = c(rep(1, 7), rep(2, 7), rep(3, 7), rep(4, 7), rep(5, 7), rep(6, 7), rep(7, 7)),
						houseideo_NA = seq(1, 7, 1))
						
pred.data.cong$house.toocons <- ifelse(pred.data.cong$selfideo_NA < pred.data.cong$houseideo_NA, 1, 0)
pred.data.cong$house.toolib <- ifelse(pred.data.cong$selfideo_NA > pred.data.cong$houseideo_NA, 1, 0)						
pred.data.cong$house.dist <- pred.data.cong$selfideo_NA - pred.data.cong$houseideo_NA
pred.data.cong$house.dist2 <- pred.data.cong$house.dist*pred.data.cong$house.dist


preds.2018.cong <- data.frame(pred.data.cong, predict(cong.2018.dist2, pred.data.cong, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2018)

preds.2017.cong <- data.frame(pred.data.cong, predict(cong.2017.dist2, pred.data.cong, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2017)

preds.2016.cong <- data.frame(pred.data.cong, predict(cong.2016.dist2, pred.data.cong, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2016)

preds.2015.cong <- data.frame(pred.data.cong, predict(cong.2015.dist2, pred.data.cong, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2015)

preds.2014.cong <- data.frame(pred.data.cong, predict(cong.2014.dist2, pred.data.cong, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2014)

preds.2013.cong <- data.frame(pred.data.cong, predict(cong.2013.dist2, pred.data.cong, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2013)

preds.2012.cong <- data.frame(pred.data.cong, predict(cong.2012.dist2, pred.data.cong, se.fit = TRUE, 
	interval = "confidence", level = 0.95), year = 2012)

all.preds.cong <- rbind(preds.2018.cong, preds.2017.cong, preds.2016.cong, preds.2015.cong, preds.2014.cong, preds.2013.cong, preds.2012.cong)


all.preds.small.cong <- all.preds.cong[all.preds.cong$selfideo_NA %in% c(1, 4, 7), ]
all.preds.small.cong$house.distcopy <- all.preds.small.cong$house.dist

all.preds.small.cong$Ideology <- all.preds.small.cong$selfideo_NA
all.preds.small.cong$Ideology[all.preds.small.cong$Ideology == 1] <- "Very Liberal"
all.preds.small.cong$Ideology[all.preds.small.cong$Ideology == 4] <- "Moderate"
all.preds.small.cong$Ideology[all.preds.small.cong$Ideology == 7] <- "Very Conservative"

all.preds.small.cong$Ideology <- all.preds.small.cong$selfideo_NA
all.preds.small.cong$Ideology[all.preds.small.cong$Ideology == 1] <- "Very Liberal"
all.preds.small.cong$Ideology[all.preds.small.cong$Ideology == 4] <- "Moderate"
all.preds.small.cong$Ideology[all.preds.small.cong$Ideology == 7] <- "Very Conservative"

all.preds.small.cong <- all.preds.small.cong[all.preds.small.cong$pid %in% c(1, 4, 7), ]
all.preds.small.cong$PID <- all.preds.small.cong$pid
all.preds.small.cong$PID[all.preds.small.cong$PID == 1] <- "Strong Democrat"
all.preds.small.cong$PID[all.preds.small.cong$PID == 4] <- "Independent"
all.preds.small.cong$PID[all.preds.small.cong$PID == 7] <- "Strong Republican"

all.preds.small.cong$PID <- factor(all.preds.small.cong$PID, levels = c("Strong Democrat", "Independent", "Strong Republican"))
all.preds.small.cong$Ideology <- factor(all.preds.small.cong$Ideology, levels = c("Very Liberal", "Moderate", "Very Conservative"))

all.preds.smallest.cong <- all.preds.small.cong[all.preds.small.cong$house.dist %in% c(-6, -3, 0, 3, 6),]

## Now organize by ideology
# -3
all.preds.smallest.cong$Ideologyorg <- all.preds.smallest.cong$selfideo_NA
all.preds.smallest.cong$Ideologyorg <- all.preds.smallest.cong$Ideologyorg - 4

all.preds.smallest.cong$Ideologyorg[all.preds.smallest.cong$house.distcopy == -6 & all.preds.smallest.cong$Ideologyorg == -3] <- -4
all.preds.smallest.cong$Ideologyorg[all.preds.smallest.cong$house.distcopy == 0 & all.preds.smallest.cong$Ideologyorg == -3] <- -2

all.preds.smallest.cong$Ideologyorg[all.preds.smallest.cong$house.distcopy == -3 & all.preds.smallest.cong$Ideologyorg == 0] <- -1
all.preds.smallest.cong$Ideologyorg[all.preds.smallest.cong$house.distcopy == 3 & all.preds.smallest.cong$Ideologyorg == 0] <- 1

all.preds.smallest.cong$Ideologyorg[all.preds.smallest.cong$house.distcopy == 0 & all.preds.smallest.cong$Ideologyorg == 3] <- 2
all.preds.smallest.cong$Ideologyorg[all.preds.smallest.cong$house.distcopy == 6 & all.preds.smallest.cong$Ideologyorg == 3] <- 4

# Now shift the party ids
all.preds.smallest.cong$Ideologyorg[all.preds.smallest.cong$pid == 1] <- all.preds.smallest.cong$Ideologyorg[all.preds.smallest.cong$pid == 1] - 0.3
all.preds.smallest.cong$Ideologyorg[all.preds.smallest.cong$pid == 7] <- all.preds.smallest.cong$Ideologyorg[all.preds.smallest.cong$pid == 7] + 0.3

all.preds.smallest.cong$Congress <- all.preds.smallest.cong$house.distcopy
all.preds.smallest.cong$Congress[all.preds.smallest.cong$Congress == -6] <- "Way Too Conservative"
all.preds.smallest.cong$Congress[all.preds.smallest.cong$Congress == -3] <- "Too Conservative"
all.preds.smallest.cong$Congress[all.preds.smallest.cong$Congress == 0] <- "Just Right"
all.preds.smallest.cong$Congress[all.preds.smallest.cong$Congress == 3] <- "Too Liberal"
all.preds.smallest.cong$Congress[all.preds.smallest.cong$Congress == 6] <- "Way Too Liberal"

all.preds.smallest.cong$Congress <- factor(all.preds.smallest.cong$Congress, levels = c("Way Too Conservative", "Too Conservative", "Just Right", "Too Liberal", "Way Too Liberal"))

all.preds.smallest.cong$Ideologyorgmore <- all.preds.smallest.cong$Ideologyorg
all.preds.smallest.cong$Ideologyorgmore[all.preds.smallest.cong$Ideologyorgmore < -1.5] <- all.preds.smallest.cong$Ideologyorgmore[all.preds.smallest.cong$Ideologyorgmore < -1.5] - 1
all.preds.smallest.cong$Ideologyorgmore[all.preds.smallest.cong$Ideologyorgmore > 1.5] <- all.preds.smallest.cong$Ideologyorgmore[all.preds.smallest.cong$Ideologyorgmore > 1.5] + 1

all.preds.smallest.cong.2012 <- all.preds.smallest.cong[all.preds.smallest.cong$year == 2012,]
all.preds.smallest.cong.2013 <- all.preds.smallest.cong[all.preds.smallest.cong$year == 2013,]
all.preds.smallest.cong.2014 <- all.preds.smallest.cong[all.preds.smallest.cong$year == 2014,]
all.preds.smallest.cong.2015 <- all.preds.smallest.cong[all.preds.smallest.cong$year == 2015,]
all.preds.smallest.cong.2016 <- all.preds.smallest.cong[all.preds.smallest.cong$year == 2016,]
all.preds.smallest.cong.2017 <- all.preds.smallest.cong[all.preds.smallest.cong$year == 2017,]
all.preds.smallest.cong.2018 <- all.preds.smallest.cong[all.preds.smallest.cong$year == 2018,]

cong.2012 <- ggplot(data = all.preds.smallest.cong.2012, aes(Ideologyorgmore, fit.fit, shape = Congress, color = PID)) + 
	ylim(1.3, 2.5) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "Congress Distance", x = "Self Ideology", y = "Predicted Congress Approval: 2012")

cong.2013 <- ggplot(data = all.preds.smallest.cong.2013, aes(Ideologyorgmore, fit.fit, shape = Congress, color = PID)) + 
	ylim(1.3, 2.5) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "Congress Distance", x = "Self Ideology", y = "Predicted Congress Approval: 2013")

cong.2014 <- ggplot(data = all.preds.smallest.cong.2014, aes(Ideologyorgmore, fit.fit, shape = Congress, color = PID)) + 
	ylim(1.3, 2.5) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "Congress Distance", x = "Self Ideology", y = "Predicted Congress Approval: 2014")

cong.2015 <- ggplot(data = all.preds.smallest.cong.2015, aes(Ideologyorgmore, fit.fit, shape = Congress, color = PID)) + 
	ylim(0.8, 2.5) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "Congress Distance", x = "Self Ideology", y = "Predicted Congress Approval: 2015")

cong.2016 <- ggplot(data = all.preds.smallest.cong.2016, aes(Ideologyorgmore, fit.fit, shape = Congress, color = PID)) + 
	ylim(0.8, 2.5) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "Congress Distance", x = "Self Ideology", y = "Predicted Congress Approval: 2016")

cong.2017 <- ggplot(data = all.preds.smallest.cong.2017, aes(Ideologyorgmore, fit.fit, shape = Congress, color = PID)) + 
	ylim(0.8, 2.5) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "Congress Distance", x = "Self Ideology", y = "Predicted Congress Approval: 2017")

cong.2018 <- ggplot(data = all.preds.smallest.cong.2018, aes(Ideologyorgmore, fit.fit, shape = Congress, color = PID)) + 
	ylim(0.8, 2.5) +
	geom_errorbar(aes(ymin = fit.lwr, ymax = fit.upr), width = 0.1) +	
	geom_point(size = 2) +
	geom_vline(xintercept = -2, lwd = 2) +
	geom_vline(xintercept = 2, lwd = 2) +
	scale_colour_grey(start = 0.8, end = 0) + 	
	theme_bw() + 
	scale_x_continuous(minor_breaks = NULL, breaks = c(-4, 0, 4), label = c("VL", "Mod.", "VC")) + 
	labs(color = "Party ID", shape = "Congress Distance", x = "Self Ideology", y = "Predicted Congress Approval: 2018")

#pdf("/Users/scj0014/Myfiles/Research/SCOTUS Approval/houseappsNA.pdf")
ggarrange(cong.2015, cong.2016, cong.2017, cong.2018,
	ncol = 2, nrow = 2, common.legend = TRUE, legend = "right")
dev.off()



stargazer(cong.2015.dist2, cong.2016.dist2, cong.2017.dist2, cong.2018.dist2, 
	covariate.labels = c("SCOTUS Approval", "President Approval", 
		"Born Again", "Female", "Age", "Income", "Education", "Party ID",
		"Self Ideology (Liberal)", "House Too Conservative (Dummy)", 
		"House Too Liberal (Dummy)", "House Distance (Squared)",
		"House Too Conservative (Dummy):House Distance (Squared)"))


